package com.honghu.villagepost.activity.fragment

import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.honghu.villagepost.activity.GoodsDetailsActivity
import com.honghu.villagepost.adapter.GoodsAdapter
import com.honghu.villagepost.api.ApiService
import com.honghu.villagepost.base.BaseListFragmet
import com.honghu.villagepost.callback.DialogCallback
import com.honghu.villagepost.callback.LzyResponse
import com.honghu.villagepost.constant.Constants
import com.honghu.villagepost.entity.Goods
import com.honghu.villagepost.entity.HomeGoodsList
import com.honghu.villagepost.util.DensityUtil
import com.honghu.villagepost.widget.decoration.GridSpacingItemDecoration
import com.lzy.okgo.model.Response

/**
 * 首页猜你喜欢列表
 */
class HomeLikeFragment : BaseListFragmet<Goods>() {

    private var shopType: Int = 0

    private val goodsAdapter: GoodsAdapter by lazy {
        GoodsAdapter()
    }

    companion object {
        val DETAIL_REQUEST_CODE: Int = 1

        fun newInstance(shopType: Int): HomeLikeFragment {
            val fragment = HomeLikeFragment()
            var bundle = Bundle()
            bundle.putInt(Constants.TYPE, shopType)
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun getEnableRefresh(): Boolean {
        return false
    }

    override fun initData() {
        super.initData()

        if (arguments != null) {
            shopType = arguments!!.getInt(Constants.TYPE, 0);
        }

        onRefresh(refreshLayout)
    }

    override fun bindRecyclerView(recLv: RecyclerView?) {
        recLv!!.layoutManager = GridLayoutManager(activity, 2)
        recLv.addItemDecoration(GridSpacingItemDecoration(2, DensityUtil.dp2px(8f), true))
        goodsAdapter.setOnItemClickListener { adapter, view, position ->
            var item = goodsAdapter.getItem(position)
            GoodsDetailsActivity.self(activity, item!!.id)
        }
        recLv!!.adapter = goodsAdapter
    }

    override fun loadData(page: Int, pageCount: Int) {
        ApiService.getGoodsLike(this, shopType, page, object : DialogCallback<LzyResponse<HomeGoodsList>>() {
            override fun onSuccess(response: Response<LzyResponse<HomeGoodsList>>?) {
                onFinishRefresh()
                if (shopType == 0){
                    onGetDataSuc(response!!.body().data.goodsList)
                }else{
                    onGetDataSuc(response!!.body().data.records)
                }
            }

            override fun onError(response: Response<LzyResponse<HomeGoodsList>>?) {
                super.onError(response)
                onFinishRefresh()
            }
        })
    }

    private fun onFinishRefresh() {
        if (parentFragment is HomeFragment) {
            (parentFragment as HomeFragment).onFinishRefresh()
        } else if (parentFragment is VillageFragment) {
            (parentFragment as VillageFragment).onFinishRefresh()
        }
    }

    override fun getData(): MutableList<Goods> {
        return goodsAdapter.data
    }

    override fun bindData(isRefresh: Boolean, list: MutableList<Goods>?) {
        if (isRefresh) goodsAdapter.setNewData(list) else goodsAdapter.addData(list!!)
    }

    fun setEnableRefresh(enableRefresh: Boolean) {
    }

    fun onAutoRefresh() {
        onRefresh(refreshLayout)
    }
}